package netframe;

import android.util.Log;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.functions.Func1;

/**
 * Created by Administrator on 2018/4/4.
 */

public class NetReconnectFunc1 implements Func1<Observable<? extends Throwable>, Observable<?>> {
    private final int maxRetries = 3;
    private final int retryDelayMillis = 300;
    private int retryCount;

    @Override
    public Observable<?> call(Observable<? extends Throwable> observable) {
        return observable.flatMap(new Func1<Throwable, Observable<?>>() {
            @Override
            public Observable<?> call(Throwable throwable) {
                Log.e("TAG", "call: " + throwable.getMessage());
                if (++retryCount <= maxRetries) {
                    return Observable.timer(retryDelayMillis, TimeUnit.MILLISECONDS);
                }
                return Observable.error(throwable);
            }
        });
    }
}
